package cn.anecansaitin.firecrafting.client.crafting;

import cn.anecansaitin.firecrafting.api.common.crafting.IFireRecipe;
import cn.anecansaitin.firecrafting.api.client.crafting.IJEIRecipeRender;
import cn.anecansaitin.firecrafting.client.util.RenderHelper;
import cn.anecansaitin.firecrafting.common.crafting.FireEntityRecipe;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FireEntityRecipeRender implements IJEIRecipeRender {
    //这个类只需要一个就可以了，因此将构造函数设为private并提供一个静态实现。
    public static final FireEntityRecipeRender INSTANCE = new FireEntityRecipeRender();
    //配方缓存
    private IFireRecipe cacheRecipe;
    //实体缓存
    private List<Entity> cacheEntity;

    private FireEntityRecipeRender() {
    }

    @Override
    public void drawBeforeFire(IFireRecipe recipe, PoseStack stack, double mouseX, double mouseY) {
        if (recipe instanceof FireEntityRecipe) {
            //获取渲染端世界
            ClientLevel world = Minecraft.getInstance().level;
            List<Entity> entities = new ArrayList<>();
            //渲染实体缓存
            if (recipe == cacheRecipe) {
                entities = cacheEntity;
            } else {
                //获取配方内实体
                for (EntityType<?> output : ((FireEntityRecipe) recipe).getOutputs()) {
                    entities.add(output.create(Objects.requireNonNull(world)));
                }
                //更新缓存
                cacheEntity = entities;
                cacheRecipe = recipe;
            }
            //把实体渲染到屏幕，并根据数量自动调整渲染位置
            int entitiesSize = entities.size();
            float middle = entitiesSize % 2 > 0 ? entitiesSize >> 1 : (entitiesSize >> 1) - 0.5f;
            for (int i = 0; i < entitiesSize; i++) {
                Entity entity = entities.get(i);
                RenderHelper.renderEntity(entity, stack, (int) Math.floor(140 + (middle - i) * 15), (int) Math.floor(18 + 19.5 * entity.getBoundingBox().maxY / 2), 20, -30);
            }
        }
    }

    @Override
    public List<Component> getTooltip(IFireRecipe recipe, double mouseX, double mouseY) {
        ArrayList<Component> list = new ArrayList<>();
        if (recipe instanceof FireEntityRecipe) {
            //获取产物
            List<EntityType<?>> entities = ((FireEntityRecipe) recipe).getOutputs();
            int entitiesSize = entities.size();
            //根据鼠标位置返回对应实体名，区域算法与实体渲染基本一致
            float middle = entitiesSize % 2 > 0 ? entitiesSize >> 1 : (entitiesSize >> 1) - 0.5f;
            for (int i = 0; i < entitiesSize; i++) {
                int x1 = (int) Math.floor(140 + (middle - i) * 15);
                int x2 = (int) Math.floor(140 + (middle - i + 1) * 15);
                if (mouseX >= x1 && mouseX < x2 && mouseY >= 26 && mouseY <= 56) {
                    list.add(entities.get(i).getDescription());
                }
            }
        }
        return list;
    }
}
